package com.winsoar.cams.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.winsoar.cams.entity.DetailedSchedule;
import com.winsoar.cams.entity.Person;
import com.winsoar.cams.entity.Schedule;
import com.winsoar.cams.entity.User;
import com.winsoar.cams.service.ClasstableService;
import com.winsoar.cams.service.DetailedScheduleService;
import com.winsoar.cams.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author TangYaD
 * @Date 2021/2/6 11:51
 * @Version 1.0
 */
@Controller
@RequestMapping("schedule")
public class ScheduleControlle {
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private ClasstableService classtableService;
    @Autowired
    private DetailedScheduleService detailedScheduleService;

    /*跳转添加课表页*/
    @RequestMapping("/toInsertschedule")
    public ModelAndView toInsertStudent(Long personId) {
        ModelAndView view = new ModelAndView();
        view.setViewName("h-ui/schedule/insert");
        List<Map<String, Object>> classlist = classtableService.queryAllInfo();
        view.addObject("classlist", classlist);
        return view;
    }

    /*添加课表*/
    @ResponseBody
    @RequestMapping("/Insertschedule")
    public HashMap<String, Object> insertstudent(Schedule schedule) {
        System.out.println(schedule);
        Schedule insert = scheduleService.insert(schedule);
        DetailedSchedule detailedSchedule = new DetailedSchedule();
        detailedSchedule.setClassId(schedule.getClassId());
        detailedSchedule.setClassRoomId((long) 1);
        detailedSchedule.setCourseId((long) 1);
        detailedSchedule.setDay(1);
        detailedSchedule.setPart(1);
        detailedSchedule.setPersonId((long) 1);
        detailedSchedule.setScheduleId(insert.getScheduleId());
        DetailedSchedule insert1 = detailedScheduleService.insert(detailedSchedule);
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg", schedule.toString());
        if (insert != null && insert1 != null) {
            map.put("sucess", "1");
            map.put("msg", "添加成功");

        } else {
            map.put("sucess", "0");
            map.put("msg", "添加失败");
        }
        return map;
    }

    //跳转课表页面
    @RequestMapping("/toIndex")
    public ModelAndView toIndex() {
        ModelAndView view = new ModelAndView();
        List<Map<String, Object>> classlist = classtableService.queryAllInfo();
        view.setViewName("h-ui/schedule/index");
        view.addObject("classlist", classlist);
        return view;
    }

    //App获取课表页面
    @RequestMapping("/mtoIndex")
    public String tomIndex(Model model) {

        List<Map<String, Object>> classlist = classtableService.queryAllInfo();
        model.addAttribute("classlist", classlist);
        return "h-ui/schedule/mindex";
    }

    //    查询课表详细
    @RequestMapping("/getScheduleList")
    @ResponseBody
    public JSONArray getScheduleList(String classId) {
        System.out.println(classId);
        List<Map<String, Object>> schedules = scheduleService.queryScheduleAll(classId);
        /*String json1=JSON.toJSONString(schedules);*/
        JSONArray jsonObject = (JSONArray) JSON.toJSON(schedules);

        return jsonObject;
    }

}
